#include "my_header.h"
#include "analysis.h"
#include "transfile.h"
#include "sendMsg.h"
int putsFile(const char* finalPath,Canalysis_t* canalysis,int netfd){
        // 拼接最新的文件路径
        //判断是否有此目录
        char query[4096];
        char dirpath[1024]={0};
        snprintf(dirpath,strlen(finalPath)-strlen(canalysis->lastname),"%s",finalPath);
        if(strlen(dirpath)==0){
            sprintf(dirpath,"/");
        }
        DEBUG_PRINT("dirpath is %s\n",dirpath);
        sprintf(query,"select fid from file_table where username = '%s' and path = '%s'",canalysis->username,dirpath);
        DEBUG_PRINT("before query %s\n",dirpath);
        DEBUG_PRINT("%x\n",canalysis->mysql);
        DEBUG_PRINT("mysql= %s\n",query);
        int qret = mysql_query(canalysis->mysql,query);
        DEBUG_PRINT("after auery %s\n",dirpath);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
            DEBUG_PRINT("putsFile mysql_query failed\n");
            exit(-1);
        }
        
        DEBUG_PRINT("dirpath is %s\n",dirpath);
        MYSQL_RES* result = mysql_store_result(canalysis->mysql);
        DEBUG_PRINT("dirpath is %s\n",dirpath);
        unsigned long rowNumber = mysql_num_rows(result);
        if(rowNumber == 0){
            // 说明没找到对应的目录
            sendMsg(netfd,"0");
            sprintf(canalysis->result,"目录不存在");
            return 0;
        }
        int fid;//存储目录fid
        
        DEBUG_PRINT("before send %s\n",dirpath);
        char buf[]="1";
        sendMsg(netfd,buf);

        DEBUG_PRINT("after %s\n",dirpath);
        MYSQL_ROW fid_t=mysql_fetch_row(result);
        fid=atoi(fid_t[0]);
        DEBUG_PRINT("dirpath is %d\n",fid);


        //hash判断,进入此步客户端序发送文件hash值
        char sha1str[41] = {0};
        recvMsg(netfd,sha1str);
        // 获取文件对应的哈希值
        // 根据哈希查找有无同名文件
        DEBUG_PRINT("sha1 is %s\n",sha1str);
        long filesize;
        bzero(query,sizeof(query));
        sprintf(query,"select filesize from file_table where hash_sha1 = %s",sha1str);
        qret = mysql_query(canalysis->mysql,query);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
            DEBUG_PRINT("putsFile mysql_query failed\n");
            exit(-1);
        }

        MYSQL_RES* result2 = mysql_store_result(canalysis->mysql);
        rowNumber = mysql_num_rows(result2);

        if(rowNumber == 0){
            // 说明没找到对应的文件，需要传上去
            // 传表
            sendMsg(netfd,"1");
            bzero(query,sizeof(query));
            
        DEBUG_PRINT("absoulatepath is %s\n",canalysis->absolutePath);
            chdir(canalysis->absolutePath);
            recvFile(netfd,&filesize);
            sprintf(query,"insert into file_table (filename,username,parent_id,hash_sha1,path,type,filesize) \
                    values ('%s','%s',%d,'%s','%s','%s',%ld)",canalysis->lastname,canalysis->username,fid,sha1str,finalPath,"f",filesize);
            int qret = mysql_query(canalysis->mysql,query);
            if(qret != 0){
                fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
                DEBUG_PRINT("putsFile mysql_query failed\n");
                exit(-1);
            }
            // 传文件
            sprintf(canalysis->result,"文件不存在，puts命令执行完毕");
        }
        else{
            // 已经有这个文件了，快传
            // 只传表
            MYSQL_ROW row=mysql_fetch_row(result);
            filesize =atol(row[0]);
            bzero(query,sizeof(query));
            sendMsg(netfd,"0");
            sprintf(query,"insert into file_table (filename,username,parent_id,hash_sha1,path,type,filesize) \
                    values ('%s','%s',%d,'%s','%s','%s',%ld)",canalysis->lastname,canalysis->username,fid,sha1str,finalPath,"f",filesize);
            int qret = mysql_query(canalysis->mysql,query);
            if(qret != 0){
                fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
                DEBUG_PRINT("putsFile mysql_query failed\n");
                exit(-1);
            }

            sprintf(canalysis->result,"文件已存在，puts命令执行完毕");
        }
        mysql_free_result(result);
        return 0;
    }


int getsFile(const char* finalPath,Canalysis_t* canalysis,int netfd)
    {   //cd到当前目录

        char request[1024]={0};
        sprintf(request,"select hash_sha1 from file_table where username = %s and type = 'f'and path = %s",canalysis->username,finalPath);
        int ret=mysql_query(canalysis->mysql,request);
        if(ret != 0)
        {
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
            DEBUG_PRINT("getsFile mysql_query failed\n");
            exit(-1);
        }
        MYSQL_RES* rows = mysql_store_result(canalysis->mysql);
        unsigned long rowNumber = mysql_num_rows(rows);
        if(rowNumber == 0){
            // 说明没找到对应的目录
            sendMsg(netfd,"0");
            sprintf(canalysis->result,"目录不存在");
            return 0;
        }
        MYSQL_ROW row =mysql_fetch_row(rows);
        sendMsg(netfd,"1");
        bzero(canalysis->result,canalysis->resultLength);
        sprintf(canalysis->result,"%s",row[0]);
        chdir(canalysis->absolutePath);
        sendFile(netfd,canalysis->result);
        return 0;
    }


